import cv2
import pytesseract

import platform

system_type = platform.system()

filepath = "D:\project\python\python_checkdata\picOCr"


def get_text_from_pic(image=None):
    # 读取图片
    # image = cv2.imread('img_1.png')
    print(system_type)
    if system_type == "Windows":
        pytesseract.pytesseract.tesseract_cmd = r'D:\app\Tesseract-OCR\tesseract.exe'  # Windows路径
    # pytesseract.pytesseract.tesseract_cmd = r'D:\app\Tesseract-OCR\tesseract.exe'  # Windows路径

    # 预处理图像（例如转换为灰度）
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # threshold_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU):ml-citation{ref="1" data="citationList"}
    threshold_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    # 使用Tesseract识别图像中的文字
    text = pytesseract.image_to_string(threshold_image)
    # text = pytesseract.image_to_string(threshold_image,lang='eng')
    # 输出识别结果
    print(text)
    data = dict()
    data["data"] = text
    data["code"] = 200
    data ["msg"] = "成功"
    return data



